2016-04-15 2 views
0

このListViewクラスでは、phpとJSONの助けを借りて私のサーバからさまざまな要素を持ってきて、リストビューを呼び出します。問題は、リストビューの特定の要素をクリックすると、別のビューに要素のIDを送信したいということです。別のビューでは、クリックした要素に関する詳細情報を公開したいと考えています。いずれにせよ、それについて考えている人はいませんか?listItemsの要素から別のViewへのIDの送信方法は、この要素のすべての情報を公開していますか?

public class Forestillinger extends AppCompatActivity { 


ListView dagensaktivitet; 
TextView txt; 
ArrayList<String> listItems = new ArrayList<String>(); 
ArrayAdapter<String> adapter; 
JSONParser jsonParser = new JSONParser(); 
JSONObject hc = null; 
JSONArray fstilling; 
public String hk = ""; 
int success; 
String msg; 
final Context c = this; 
StableArrayAdapter adb; 
private static final String url_Forestillinger = "http://barnestasjonen.no/test/db_get_forestillinger.php"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_forestillinger); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 


    dagensaktivitet = (ListView) findViewById(R.id.forestillinglist); 
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems) { 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = super.getView(position, convertView, parent); 
      TextView txt = (TextView) view.findViewById(android.R.id.text1); 
      txt.setTextColor(Color.WHITE); 
      return view; 
     } 
    }; 
    dagensaktivitet.setAdapter(adapter); 
    Log.d("i oncreate", adapter.toString()); 

    //if(!fromLocalDB()) 
    try { 
     new getShows().execute().get(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     e.printStackTrace(); 
    } 

    //JSONObject tickets = task.getJSon(); 
    adapter.notifyDataSetChanged(); 
    dagensaktivitet.setOnItemClickListener(new AdapterView.OnItemClickListener() { 


     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      Intent i = new Intent(getApplicationContext(), Forestilling.class); 
      i.putExtra("selectedItem", listItems.get(position)); 
      startActivity(i); 
     } 
    }); 

} 

public void fromExtDB() throws JSONException { 
    fstilling = hc.getJSONArray("forestillinger"); 
    for (int i = 0; i < fstilling.length(); i++) { 
     listItems.add(fstilling.getJSONObject(i).getString("tittel")+fstilling.getJSONObject(i).getString("id")); 
    } 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_forestillinger, menu); 
    return true; 

} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

public void forestillingResult(){ 
    new AlertDialog.Builder(Forestillinger.this) 
      .setTitle("OBS!!!") 
      .setMessage(msg) 
      .setCancelable(false) 
      .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

        hk = ""; 
        finish(); 
       } 
      }) 
      .show(); 
} 

private class StableArrayAdapter extends ArrayAdapter<String> { 
    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>(); 
    List<Integer> trialId; 

    public StableArrayAdapter(Context context, int textViewResourceId, List<String> objects, List<Integer> objectId) { 
     super(context, textViewResourceId, objects); 
     for (int i = 0; i < objects.size(); i++) 
      mIdMap.put(objects.get(i), i); 
     trialId = objectId; 
    } 

    public long getItemId(int position) { 
     String item = getItem(position); 
     return mIdMap.get(item); 
    } 

    public int getActualId(int position) 
    { 
     return trialId.get(position); 
    } 
} 
class getShows extends AsyncTask<String, String, String> { 

    @Override 
    protected String doInBackground(String... args) { 
     Log.d("TEST", "jeg er her"); 

     List<NameValuePair> params = new ArrayList<NameValuePair>(); 

     //params.add(new BasicNameValuePair("id", "[email protected]")); 
     //params.add(new BasicNameValuePair("type", "android")); 

     JSONObject json = jsonParser.makeHttpRequest(url_Forestillinger, "POST", params); 

     try { 
      if (json != null) { 
       success = Integer.parseInt(json.getString("success")); 
       msg = json.toString(); 
       System.out.println("Vi er her:" + msg + " " + success); 

      } 

      if (success == 1) { 
       hc = json; 
       System.out.println("jojo"+json.toString()); 

      } 
      else 
      { 

       msg=json.getString("message"); 
       System.out.println(msg); 
       msg= Html.fromHtml(msg).toString(); 
       hk = msg; 

      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    protected void onPostExecute(String file_url) { 
     //run code 
     try { 
      Log.d("TAG23", "vi er in onpostExecute"); 
      if(success == 1) { 
       fromExtDB(); 
      }else{ 
       forestillingResult(); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

} 
} 

ここには2番目のビューがあります。

public class Forestilling extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_forestilling); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    /*Bundle extras = getIntent().getExtras(); 
    Log.d("TAG:", "" + extras.getInt("TryThis"));*/ 
    Bundle extras = getIntent().getExtras(); 
    String s = extras.getString("selectedItem"); 

    Log.d("TAG:", "" + s); 


    TextView tit = (TextView)findViewById(R.id.tittel); 
    TextView dato = (TextView)findViewById(R.id.dato); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_forestilling, menu); 
    return true; 

} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

答えて

0

私はこの時点でフラグを許可されていないんだけど、これはとにかくhow can i use the number from textfield of each row in listview and make a call on this number?

の重複かもしれないが、そこに与えられた私の答えはあなたの問題を解決する可能性があります:

ここでは、私が何をすべきかです:

private class MyAdapter extends BaseAdapter { 
    private ListView listView; 
    ... 
    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     this.listView = (ListView) viewGroup; 

し、各View.OnClickListenerに私は

によってクリックされたアイテムの位置を取得することができます
@Override 
public void onClick(View view) { 
    ... 
    MyAdapter.this.listView.getPositionForView((View) view.getParent())); 
+0

コードを改善してください。私は何をロジックバック問題を知っているが、私はそれを修正することはできません:( – 1987akam

関連する問題