我是android和Java的新手.我想创建一个带有int参数的onClick方法,所以这是我的尝试:
public void randomClick(final int randomIndex)
{
private OnClickListener top_listener = new OnClickListener() {
public void onClick(View v) {
Intent top = new Intent(Main.this, ProjectDetail.class);
top.putExtra("spendino.de.ProjectDetail.position", randomIndex);
startActivity(top);
}
};
}
Run Code Online (Sandbox Code Playgroud)
但它仍然包含错误,任何人都能解决这个问题吗?
后来我想把方法设置为ImageView,它看起来或多或少会像这样 image1.randomClick(randomIndex1);
非常感谢你.我愿意接受任何解决方案.
我现在开发一个德语应用程序,它将数据从JSON提取到ListView.由于有一些特殊的字符,如üöäß,我的代码如下,这些字符显示为?.
有人可以帮我解决我的问题吗?谢谢
public class LooserSync extends IntentService {
public LooserSync() {
super("LooserSyncService");
}
@Override
protected void onHandleIntent(Intent intent) {
Database.OpenHelper dbhelper = new Database.OpenHelper(getBaseContext());
SQLiteDatabase db = dbhelper.getWritableDatabase();
DefaultHttpClient httpClient = new DefaultHttpClient();
db.beginTransaction();
HttpGet request = new HttpGet(
"http://liebenwald.spendino.net/admanager/dev/android/projects.json");
try {
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream instream = response.getEntity().getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(
instream), 8000);
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line); …Run Code Online (Sandbox Code Playgroud) 任何人都可以帮助我制作一个方法来生成随机数而无需在Android中重复?最大数量是:prjcts.size();它是我的JSON数组.返回值应该是整数.
我已经拥有的是:
int i = (int)(prjcts.size() * Math.random());我将该方法输入3次,因为我需要3个随机生成的数字.它有效,但我不知道怎么做而不重复.所以这3个数字彼此之间不一样.
谢谢
我有一个JSON文件,填充到一个活动(Main.java).
此活动显示来自我的JSON条目上的URL的3个随机图像.
我想做的是:我在我的JSON上有13个不同的条目,每当我点击显示的随机图片时,它转到另一个包含图片,标题和描述的活动(ProjectDetail.java)取决于我点击的项目进入JSON.
我有什么是通过使用额外的我不知道如何执行,因为我使用JSON.我应该top_listener在我的Main类的方法中添加什么以及我应该在ProjectDetail类中添加什么?
谢谢.
Main.java
public class Main extends Activity {
/** Called when the activity is first created. */
ArrayList<Project> prjcts=null;
private ImageThreadLoader imageLoader = new ImageThreadLoader();
private final static String TAG = "MediaItemAdapter";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prjcts = new ArrayList<Project>();
WebService webService = new WebService("http://liebenwald.spendino.net/admanager/dev/android/projects.json");
Map<String, String> params = new HashMap<String, String>();
params.put("var", "");
String response = webService.webGet("", params);
try
{
Type collectionType = new TypeToken<ArrayList<Project>>(){}.getType();
List<Project> lst= new Gson().fromJson(response, …Run Code Online (Sandbox Code Playgroud) 有人可以帮我解析 JSON 中的 HTML 标签吗?我<b>, <p>, etc.在我的 JSON 文件的值上得到了标签。而且我希望能够解析它们,因此我的应用程序上显示的文本是基于 HTML 标签显示的。
这是我的 JSON 解析代码:
void examineJSONFile()
{
try
{
String y = "";
InputStream is = this.getResources().openRawResource(R.raw.uni);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "Unicode")
);
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
String jsontext = writer.toString();
JSONArray entries = new JSONArray(jsontext);
int j;
for …Run Code Online (Sandbox Code Playgroud) 我有这个从网上下载图像的类,我需要有人帮我修改下面的代码,以便下载的图像保存在SD卡上.因为我从互联网上获取这些代码并且我是新手,所以我很无法为缓存内容优化它.
我愿意接受任何解决方案.非常感谢.
public class ImageThreadLoader {
private static final String TAG = "ImageThreadLoader";
// Global cache of images.
// Using SoftReference to allow garbage collector to clean cache if needed
private final HashMap<String, SoftReference<Bitmap>> Cache = new HashMap<String, SoftReference<Bitmap>>();
private final class QueueItem {
public URL url;
public ImageLoadedListener listener;
}
private final ArrayList<QueueItem> Queue = new ArrayList<QueueItem>();
private final Handler handler = new Handler(); // Assumes that this is started from the main (UI) thread
private Thread thread;
private …Run Code Online (Sandbox Code Playgroud) 我在下面有这个方法,根据我的JSON条目的大小生成随机数,每个数字只能来一次.我将方法强制转换3次,因为我需要生成3个随机数,它们之间应该是不同的.
有时它可以工作,但有时它会在logcat上给我这个错误: 错误java.lang.IndexOutOfBoundsException无效的位置 ,这使我的应用程序崩溃.
有人可以帮我解决我的问题吗?谢谢!
int max = prjcts.size();
List<Integer> indices = new ArrayList<Integer>(max);
for(int c = 1; c < max; ++c)
{
indices.add(c);
}
int arrIndex = (int)((double)indices.size() * Math.random());
int randomIndex1 = indices.get(arrIndex);
indices.remove(arrIndex);
int randomIndex2 = indices.get(arrIndex);
indices.remove(arrIndex);
int randomIndex3 = indices.get(arrIndex);
indices.remove(arrIndex);
Run Code Online (Sandbox Code Playgroud)
@KPBird: 所以我改变我的代码就像你告诉我的那样,但它仍然给我错误soemtime:我错过了什么吗?
Random r = new Random();
int arrIndex = r.nextInt(indices.size());
int randomIndex1 = indices.get(arrIndex);
indices.remove(arrIndex);
int randomIndex2 = indices.get(arrIndex);
indices.remove(arrIndex);
int randomIndex3 = indices.get(arrIndex);
indices.remove(arrIndex);
Run Code Online (Sandbox Code Playgroud) 我的活动上有一个复选框.我想知道每次打开我的应用程序时如何使其状态保持不变(选中/取消选中).
我有这个SQLite包含许多'项目'.每个具有SQLite数据库表属性的"项目"都显示在活动中.我想要做的是,当点击活动中的按钮时,它会更新该C_FAVORITE"项目" 的行.
我怎么能这样做?我需要在下面的更新方法中添加哪些参数?
public void makeFavorite() {
Database.Project.C_FAVORITE.update(.......);
}
Run Code Online (Sandbox Code Playgroud)
这是我在活动中显示项目属性的方式:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
loader = new ImageLoader(this);
Intent intent = getIntent();
if (intent != null) {
Uri uri = intent.getData();
if (uri != null) {
final Cursor cursor = managedQuery(uri, new String[] {
BaseColumns._ID, Database.Project.C_PROJECTTITLE, Database.Project.C_ORGANIZATIONTITLE,
Database.Project.C_PROJECTDESCRIPTION,Database.Project.C_BIGIMAGE,Database.Project.C_DONATIONAMOUNT,Database.Project.C_ADDRESS,Database.Project.C_WEBSITE,Database.Project.C_SHORTCODE,Database.Project.C_KEYWORD,Database.Project.C_PRICE,Database.Project.C_CAMPAIGNID,Database.Project.C_PAYPALEMAIL,Database.Project.C_ELVAVAILABLE}, null, null, null);
if (cursor == null) {
finish();
} else {
if (cursor.moveToFirst()) {
ImageView img = (ImageView) findViewById(R.id.project_image);
TextView project_title = (TextView)findViewById(R.id.txt_project_title);
project_title.setText(cursor.getString(1));
Run Code Online (Sandbox Code Playgroud)
......
我想在用户单击下面的按钮并且String打开EditText为空时显示该对话框.所以我做了这个方法,但不幸的是,而不是显示一个对话框,应用程序崩溃了.对话框方法没有问题,问题是IF函数没有正确读取我请求的内容.
有人有解决方案吗?
这是我的方法:
public void onClick(View v) {
if(v == launchSimplePayment) {
String amount = paymentAmount.getText().toString();
System.out.println(amount);
if (amount == "")
{
errorDialog();
}
else
{
System.out.println(amount);
// Use our helper function to create the simple payment.
PayPalPayment payment = exampleSimplePayment();
// Use checkout to create our Intent.
Intent checkoutIntent = PayPal.getInstance().checkout(payment, this, new ResultDelegate());
// Use the android's startActivityForResult() and pass in our Intent. This will start the library.
startActivityForResult(checkoutIntent, request);
}
}
Run Code Online (Sandbox Code Playgroud) 在mdpi设备上我想调用这个方法:
final float scale = getResources().getDisplayMetrics().density;
double height_px = 45 * scale + 0.5;
Run Code Online (Sandbox Code Playgroud)
但我想在hdpi设备上运行应用程序时忽略该方法,我如何确定我班级的屏幕大小?